package cn.dwong.msgGateway.service;

import cn.dwong.msgGateway.base.ApiResponse;
import cn.dwong.msgGateway.config.MsgConfig;
import cn.dwong.msgGateway.entity.TitleContent;
import cn.dwong.msgGateway.entity.User;
import cn.dwong.msgGateway.util.SingletonCacheUtil;
import cn.hutool.cache.Cache;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author 73202
 * @date 2022/3/9 20:58
 */
@Service
public class MessageService {
    @Autowired
    private MsgConfig msgConfig;

    private final Logger log = LoggerFactory.getLogger(MessageService.class);
    Cache<Object, Object> userCache = new SingletonCacheUtil().getSingletonCacheUtil();

    public boolean checkSessionId(String userName,String sessionId){
        if (userCache.get(userName) != null){
            String password = userCache.get(userName).toString();
            String sessionKey = userName+password;
            if (userCache.get(sessionKey) != null){
                if (sessionId.equals(userCache.get(sessionKey).toString())){
                    return true;
                }
            }
        }
        return false;
    }

    public String sendMsg(String qos, String acceptor_tel, TitleContent titleContent){

        JSONObject json = JSONUtil.createObj()
                .put("qos",qos)
                .put("acceptor_tel",acceptor_tel)
                .put("template_param",titleContent)
                .put("timestamp", DateUtil.now());

//        String send_sms_url = "http://127.0.0.1:8080/v2/emp/templateSms/sendSms";
        String send_sms_url = msgConfig.getSmsHost() + "/v2/emp/templateSms/sendSms";
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<JSONObject> request = new HttpEntity<>(json,headers);
        ResponseEntity<String> response = null;
        String eobj = null;
        try{
            response = restTemplate.postForEntity(send_sms_url, request , String.class );
        }catch (Exception e){
            eobj =  e.getMessage();
        }


        String resCode = null;
        try{
            if (response != null){
                JSONObject obj = JSONUtil.parseObj(response.getBody());
                if (obj != null){
                    resCode = obj.get("res_code").toString();
                }
            }else if (eobj != null){
                resCode = StrUtil.sub(eobj,0,3);
            }
        }catch (Exception e){
            return resCode;
        }

        return resCode;
    }

    public ResponseEntity getCurrentStatus(){
        String current_status_url = "http://127.0.0.1:8080/v2/emp/templateSms/currentStatus";
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForEntity(current_status_url,ResponseEntity.class);
    }
}
